package com.hejia.bulbul.scheduler;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;

import com.hejia.alauda.service.BonusService;
import com.umeng.config.UmengConfig;

/**
 * 红包到期提醒作业
 * 
 * @author hyr
 *
 */
public class BonusExpireAlertScheduler {

	private static final Log logger = LogFactory.getLog(BonusExpireAlertScheduler.class);
	
	@Resource
	private UmengConfig umengConfig;
	
	@Resource
	private BonusService bonusService;

	/**
	 * 红包到期09：00提醒
	 * @throws Exception 
	 */
	// [秒] [分] [小时] [日] [月] [周] [年]
	@Scheduled(cron = "0 0 9 * * ?")
	@Async("executor")
	public void bonusExpireAM09Alert() throws Exception {
		
		try {
			logger.info("开始9点红包过期提醒");
			
			// 红包1天到期推送消息提醒
			List<Long> bonusRecordIds = this.bonusService.findOneDayExpirePushAlertBonusRecordIds();
			for (Long bonusRecordId : bonusRecordIds) {
				this.bonusService.sendOneDayExpirePushAlert(umengConfig, bonusRecordId);
			}
			
			logger.info("结束9点红包过期提醒");
			
		} catch (Exception e) {
			logger.error("执行9点红包过期提醒失败", e);
		}
	}
	
	/**
	 * 红包到期14：00提醒
	 */
	// [秒] [分] [小时] [日] [月] [周] [年]
	@Scheduled(cron = "0 0 14 * * ?")
	@Async("executor")
	public void bonusExpireAM14Alert() {
		
		try {
			logger.info("开始执行11点红包过期提醒");
			
			// 红包1天到期短信提醒
			List<Long> bonusRecordIds = this.bonusService.findOneDayExpireSmsAlertBonusRecordIds();
			for (Long bonusRecordId : bonusRecordIds) {
				this.bonusService.sendOneDayExpireSmsAlert(bonusRecordId);
			}

			// 红包7天到期推送消息提醒
			List<Long> sevenDayBonusRecordIds = this.bonusService.findSevenDayExpirePushAlertBonusRecordIds();
			for (Long bonusRecordId : sevenDayBonusRecordIds) {
				this.bonusService.sendSevenDayExpirePushAlert(umengConfig, bonusRecordId);
			}
			
			logger.info("结束执行14点红包过期提醒");
		} catch (Exception e) {
			logger.error("执行14点红包过期提醒失败", e);
		}
	}
}
